package gui;

import java.util.Observable;
import java.util.Observer;
import java.util.StringTokenizer;

import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import logic.ChessGame;

public class MainGUI extends HBox implements Observer {

	private ChessGUI chessGUI;
	private ChessGame chessGame;
	private VBox vBox;
	private VBox logVBox;
	private ScrollPane logAIScrollPane, logYouScrollPane;
	private TextArea logAITextArea, logYouTextArea;
	private Label gameState;
	private ProgressBar timeLeftProgressBar;
	private ImageView leftCornerImage;
	private FlowPane timeLeftFlowPane;
	private StringBuffer logAIContent, logYouContent;

	private VBox left;
	private VBox center;
	private VBox right;

	private VBox aiBox;
	private VBox youBox;

	public MainGUI(ChessGUI chessGUI) {
		this.chessGUI = chessGUI;
		this.chessGame = this.chessGUI.getChessGame();
		// this.chessGUI.setChessGame(chessGame);
		this.initComponent();
		System.out.println(chessGUI.getPrefHeight());
		this.getChildren().add(left);
		this.getChildren().add(center);
		this.getChildren().add(right);
	}

	private void initComponent() {

		// right

		logAIContent = new StringBuffer();
		logAITextArea = new TextArea("log here:");
		logAITextArea.setFont(Font.font("Tahoma", FontPosture.ITALIC, 12));

		logAIScrollPane = new ScrollPane(logAITextArea);

		logVBox = new VBox();
		logVBox.setPrefWidth(300);
		logVBox.setPadding(new Insets(10));
		logVBox.setSpacing(10);

		aiBox = new VBox();
		aiBox.setPrefHeight(200);
		Label log = new Label("AI LOG:");
		log.setFont(Font.font("Tahoma", FontWeight.BOLD, 14));
		aiBox.getChildren().add(log);
		aiBox.getChildren().add(logAIScrollPane);

		logYouContent = new StringBuffer();
		logYouTextArea = new TextArea("your log here:");
		logYouTextArea.setFont(Font.font("Tahoma", FontPosture.ITALIC, 12));

		logYouScrollPane = new ScrollPane(logYouTextArea);

		youBox = new VBox();
		youBox.setPrefHeight(200);
		Label youLog = new Label("YOUR LOG:");
		youLog.setFont(Font.font("Tahoma", FontWeight.BOLD, 14));
		youBox.getChildren().add(youLog);
		youBox.getChildren().add(logYouScrollPane);

		logVBox.getChildren().add(aiBox);
		logVBox.getChildren().add(youBox);
		right = new VBox();
		right.getChildren().add(logVBox);
		// left
		leftCornerImage = new ImageView(new Image("images/leftcorner.jpg"));
		leftCornerImage.setStyle("-fx-border-radius: 5;");

		gameState = new Label(chessGame.showGameState());
		gameState.setFont(Font.font("Tahoma", FontWeight.MEDIUM, 16));
		gameState.setPadding(new Insets(30, 0, 0, 0));

		vBox = new VBox();
		vBox.setAlignment(Pos.TOP_CENTER);
		vBox.setPadding(new Insets(10));
		vBox.setSpacing(40);
		vBox.setPrefWidth(200);
		vBox.setMinWidth(200);
		vBox.setMaxWidth(200);

		Button exitButton = new Button("Exit");
		exitButton.setFont(Font.font("Tahoma", FontWeight.BOLD, 22));
		exitButton.setPrefSize(180, 60);
		exitButton.setOnAction(new EventHandler<ActionEvent>() {

			@Override
			public void handle(ActionEvent event) {
				// TODO Auto-generated method stub
				System.exit(9);
			}
		});

		Label turnLabel = new Label("TURN:");
		turnLabel.setFont(Font.font("Tahoma", FontWeight.BOLD, 16));

		vBox.getChildren().add(leftCornerImage);
		vBox.getChildren().add(turnLabel);
		vBox.getChildren().add(gameState);

		vBox.getChildren().add(exitButton);
		left = new VBox();
		left.getChildren().add(vBox);
		// bottom
		timeLeftProgressBar = new ProgressBar(1);
		timeLeftProgressBar
				.setStyle("-fx-background-color: linear-gradient(from 0px .75em to .75em 0px,repeat,-fx-accent 0%,-fx-accent 49%,derive(-fx-accent, 30%) 50%,derive(-fx-accent, 30%) 99%);");
		timeLeftProgressBar.setPrefSize(200, 30);
		timeLeftProgressBar.autosize();
		timeLeftFlowPane = new FlowPane();
		timeLeftFlowPane.setAlignment(Pos.CENTER_LEFT);
		timeLeftFlowPane.setPadding(new Insets(10));
		Label timeleft = new Label("Time left:");
		timeLeftFlowPane.prefHeight(100);
		// timeLeftFlowPane.
		timeLeftFlowPane.getChildren().add(timeleft);
		timeLeftFlowPane.getChildren().add(timeLeftProgressBar);
		center = new VBox();
		center.getChildren().add(chessGUI);
		center.getChildren().add(timeLeftFlowPane);
	}

	public void updateGameStateString() {
		this.gameState.setText(chessGame.showGameState());
	}

	@Override
	public void update(Observable o, Object arg) {
		// TODO Auto-generated method stub
		Platform.runLater(new Runnable() {
			@Override
			public void run() {
				String input = (String) arg;
				StringTokenizer st = new StringTokenizer(input, "|");
				String command = st.nextToken();
				String value = st.nextToken();
				if (command.equalsIgnoreCase("gamestate")) {
					gameState.setText(value);
				} else if (command.equalsIgnoreCase("timeleft")) {

				} else if (command.equalsIgnoreCase("logai")) {
					logAIContent.append(value + "\n");
					logAITextArea.setText(logAIContent.toString());
				} else if (command.equalsIgnoreCase("logyou")) {
					logYouContent.append(value + "\n");
					logYouTextArea.setText(logYouContent.toString());
				}
			}
		});
	}
}
